package com.qigao.mall.web.controller.app.user;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.BonusDetailService;
import com.qigao.mall.api.ProfitDetailService;
import com.qigao.mall.api.SettleDetailService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.vo.admin.merchant.BonusCondVo;
import com.qigao.mall.web.vo.admin.merchant.ProfitCondVo;
import com.qigao.mall.web.vo.admin.merchant.SettleCondVo;
import com.qigao.mall.web.vo.app.SettleReportCondVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/app/user/report/")
@Api(tags = "APP用户报表API" ,value = "app用户报表API")
public class AppReportController {

    private static Logger logger= LoggerFactory.getLogger(AppReportController.class);
    @Resource
    private SettleDetailService settleDetailService;

    @PostMapping("/settleDetail")
    @ApiOperation(value = "结算单明细", notes = "结算单明细")
    public Result<SettleReportResultDto> settleReport(@RequestBody SettleReportCondVo settleReportCondVo, HttpServletRequest request){
        try{
            //参数校验
            SettleQueryDto settleQueryDto=new SettleQueryDto();
            settleQueryDto.setOwnerNo(RequestUtils.getCurrentUserNo(request));
            String settleMonth=settleReportCondVo.getSettleMonth();
            if(StringUtils.isEmpty(settleMonth)){
                settleMonth=DateUtil.getNowMonthStr();
            }
            settleQueryDto.setSettleMonth(settleMonth);
            String dateBegin=settleMonth+"-01 00:00:00";
            settleQueryDto.setDateBegin(dateBegin);
            String dateEnd=DateUtil.getDateTimeFormat(DateUtil.addMonth(DateUtil.getDateTimeFormat(dateBegin),1));
            settleQueryDto.setDateEnd(dateEnd);
            return  settleDetailService.settleReport(settleQueryDto);
        }catch (Exception e){
            logger.error("[结算单明细]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @Resource
    private ProfitDetailService profitDetailService;

    @PostMapping("/profitDetail")
    @ApiOperation(value = "分润明细", notes = "分润明细")
    public Result<PageInfo<ProfitDetailResultDto>> listPage(@RequestBody ProfitCondVo profitCondVo, HttpServletRequest request){
        try{
            //参数校验
            ProfitCondDto profitCondDto=new ProfitCondDto();
            BeanUtils.copyProperties(profitCondVo,profitCondDto);
            profitCondDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            if(StringUtils.isNotEmpty(profitCondVo.getDateBegin())){
                profitCondDto.setDateBegin(profitCondVo.getDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(profitCondVo.getDateEnd())){
                profitCondDto.setDateEnd(profitCondVo.getDateEnd()+" 23:59:59");
            }
            return  profitDetailService.listPage(profitCondDto);
        }catch (Exception e){
            logger.error("[分润明细]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @Resource
    private BonusDetailService bonusDetailService;

    @PostMapping("/bonusDetail")
    @ApiOperation(value = "分红明细", notes = "分红明细")
    public Result<PageInfo<BonusDetailResultDto>> listPage(@RequestBody BonusCondVo bonusCondVo, HttpServletRequest request){
        try{
            //参数校验
            BonusCondDto bonusCondDto=new BonusCondDto();
            BeanUtils.copyProperties(bonusCondVo,bonusCondDto);
            bonusCondDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            if(StringUtils.isNotEmpty(bonusCondVo.getDateBegin())){
                bonusCondDto.setDateBegin(bonusCondVo.getDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(bonusCondVo.getDateEnd())){
                bonusCondDto.setDateEnd(bonusCondVo.getDateEnd()+" 23:59:59");
            }
            return  bonusDetailService.listPage(bonusCondDto);
        }catch (Exception e){
            logger.error("[分红明细]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


}
